#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "ASGChooseCityController.h"
#import "TDMineController.h"
#import "TBRHomeViewC.h"
//#import "TBRTextMapController.h"
#import "TBROrderListC.h"
#import "TBRRegisterController.h"
#import "UIColor+Hex.h"
#import "TBRLoginController.h"
#import "TBRRentController.h"
#import "TBRTextPickerC.h"

#import "TDOrderListC.h"
#define sFirstTimeLaunchedMarkKey               @"firstTimeLaunchedMark"

@interface BaseTabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>
{
    BOOL _isAppProfileVisible;
}
@end

@implementation BaseTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.barTintColor = [UIColor blackColor];
    self.delegate = self;
    [self addClientChildControllers];
}

// MARK: -  添加子控制器-------
- (void)addClientChildControllers {
    TBRHomeViewC *homeC = [[TBRHomeViewC alloc] init];
    [self setUpOneChildController:homeC Title:@"首页" ImageName:@"办公" SelectImageName:@"" tag:0];
    
    TBROrderListC *orderC = [TBROrderListC new];
    TDOrderListC *tdOrderListC = [[TDOrderListC alloc] init];
    [self setUpOneChildController:tdOrderListC Title:@"订单" ImageName:@"办公" SelectImageName:@"" tag:1];
//    ASGChooseCityController *chooseCityController = [[ASGChooseCityController alloc] init];
//    [self setUpOneChildController:chooseCityController Title:@"选择" ImageName:@"办公" SelectImageName:@""];
    //个人
    TDMineController *mineC = [TDMineController new];
    [self setUpOneChildController:mineC Title:@"我的" ImageName:@"我" SelectImageName:@"" tag:2];
}


//添加一个子控制器
- (void)setUpOneChildController:(UIViewController *)vc Title:(NSString *)title ImageName:(NSString *)imageName SelectImageName:(NSString *)selectImageName tag:(NSInteger)tag{
    vc.title = title;
    vc.tabBarItem.tag = tag;
    
    if (tag == 1) {
//        vc.tabBarItem.badgeValue = @"1";
        if (@available(iOS 10.0, *)) {
//            vc.tabBarItem.badgeColor = [UIColor blueColor];
        } else {
            // Fallback on earlier versions
        }
    }
    
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectImageName];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    if (tag == 0) {
        TBRRentController *rentC = [[TBRRentController alloc] init];
        rentC.selectedIndex = tag + 1;//传递的值应该是多1的
        [nav pushViewController:rentC animated:NO];
    }
    [self addChildViewController:nav];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark - UITabBarControllerDelegate
//这个方法根据官方文档解释意思就是点击下面的tabBar的按钮时候  根据BOOL值来判断是否处于可继续点击状态
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
    if (viewController.tabBarItem.tag == 1){
        NSString *isLogin = [HelpObject changeNull:[AppSetting shareAppSetting].isLogin];
        if (![isLogin isEqualToString:@"1"]) {
            TBRLoginController *loginC = [[TBRLoginController alloc] init];
            BaseNavigationController *loginNav = [[BaseNavigationController alloc] initWithRootViewController:loginC];
            [self presentViewController:loginNav animated:YES completion:nil];
            return NO;
        }else
        {
            return YES;
        }
        
    } else
    {
        return YES;
    }
    
}

@end
